package com.cooba.content_frament;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cooba.R;
import com.cooba.video_frament.FilmFrament;
import com.cooba.video_frament.MiliTaryFrament;
import com.cooba.video_frament.SmileFrament;
import com.cooba.video_frament.TechnologyFrament;
import com.cooba.video_frament.VarietyFrament;
import com.cooba.video_frament.VideoHotFrament;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/12/7.
 * 视频
 */
public class VideoFrament extends Fragment {

    private static String[] titles = new String[]{"热门", "电影", "爆笑", "综艺", "科技", "军事"};

    private ArrayList<Fragment> fragList;
    private ViewPager mViewPager;

    private MyTabFragmentAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Nullable
    private View contentview;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if (contentview == null) {
            contentview = initview(inflater);
        }
        //缓存的rootView需要判断是否已经被加过parent， 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) contentview.getParent();
        if (parent != null) {
            parent.removeView(contentview);
        }
        return contentview;
    }

    //初始化控件
    private View initview(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.video_frament1, null, false);
        mViewPager = (ViewPager) view.findViewById(R.id.mViewPager_video);
        // 初始化
        initFragment();
        mAdapter = new MyTabFragmentAdapter(fragList, titles, getFragmentManager(), getContext());
        mViewPager.setAdapter(mAdapter);
        // 将ViewPager和TabLayout绑定
        TabLayout tablayout = (TabLayout) view.findViewById(R.id.tablayout_video);
        tablayout.setupWithViewPager(mViewPager);

        return view;
    }


    /**
     * Created by lt on 2015/12/14.
     */
    public class MyTabFragmentAdapter extends FragmentPagerAdapter {

        private final String[] titles;
        private Context context;
        private List<Fragment> fragments;

        public MyTabFragmentAdapter(List<Fragment> fragments, String[] titles, FragmentManager fm, Context context) {
            super(fm);
            this.context = context;
            this.fragments = fragments;
            this.titles = titles;
        }


        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    }

    /**
     * 初始化 Fragment
     */
    public void initFragment() {
        fragList = new ArrayList<Fragment>();
        VideoHotFrament fragment1 = new VideoHotFrament();
        FilmFrament fragment2 = new FilmFrament();
        SmileFrament fragment3 = new SmileFrament();
        VarietyFrament fragment4 = new VarietyFrament();
        TechnologyFrament fragment5 = new TechnologyFrament();
        MiliTaryFrament fragment6 = new MiliTaryFrament();
        fragList.add(fragment1);
        fragList.add(fragment2);
        fragList.add(fragment3);
        fragList.add(fragment4);
        fragList.add(fragment5);
        fragList.add(fragment6);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        Log.d("wei--", "Video:" + isVisibleToUser);

    }
//    @Override
//    public void onDestroyView() {
//        super.onDestroyView();
//        for (int i=0;i<fragList.size();i++) {
//            if (fragList.get(i) != null) {
//                FragmentManager f = getFragmentManager();
//                if (f != null && !f.isDestroyed()) {
//                    final FragmentTransaction ft = f.beginTransaction();
//                    if (ft != null) {
//                        ft.remove(fragList.get(i)).commit();
//                    }
//                }
//            }
//        }
//    }
}
